package com.shouhuobao.take.parcel;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.shouhuobao.station.R;
import com.shouhuobao.take.input.ParcelInputActivity;
import com.shouhuobao.take.model.ContactPeopleBean;
import com.shouhuobao.take.utils.StringUtils;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;

/**
 * Created by joker on 2017/7/13.
 */
public class ParcelInputPhoneAdapter extends BaseAdapterExt<ContactPeopleBean> implements Filterable {

    private ArrayFilter mFilter;

    public ParcelInputPhoneAdapter(ArrayList<ContactPeopleBean> items, Activity context) {
        super(items, context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_parcel_input_popwin, parent, false);
            holder.nameTV = (TextView) convertView.findViewById(R.id.name_TV);
            holder.phoneTV = (TextView) convertView.findViewById(R.id.phone_TV);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ContactPeopleBean item = items.get(position);
        holder.nameTV.setText(item.getName());
        holder.phoneTV.setText(item.getPhone());
        return convertView;
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

    class ViewHolder {
        TextView nameTV;
        TextView phoneTV;
    }

    private class ArrayFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                results.values = null;
                results.count = 0;
            } else {
                String constraintString = constraint.toString();
                if (constraintString.length() >= 1) {
                    ArrayList<ContactPeopleBean> list = new ArrayList<>();
                    for (int i = 0; i < ((ParcelInputActivity) context).allList.size(); i++) {
                        ContactPeopleBean bean = ((ParcelInputActivity) context).allList.get(i);
                        if (bean != null && !StringUtils.isEmpty(bean.getPhone())) {
                            if (bean.getPhone().contains(constraintString)) {
                                list.add(bean);
                            }
                        }
                    }
                    results.values = list;
                    results.count = list.size();
                }
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results.count > 0) {
                ArrayList<ContactPeopleBean> list = (ArrayList<ContactPeopleBean>) results.values;
                if (list != null) {
                    ((ParcelInputActivity) context).setAdapterData(list);
                    notifyDataSetChanged();
                }
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}
